using System.Collections;
using System.Collections.Generic;

namespace ColladaDotNet.SimpleMesh
{
	public class IndexBuffer : IEnumerable
	{
		private readonly VertexBuffer vb;

		public IndexBuffer(VertexBuffer vb)
		{
			this.vb = vb;
		}
		List<Triangle> items = new List<Triangle>();

		public int TriangleCount
		{
			get
			{
				return items.Count;
			}
		}

		public int AddTriangle(Vertex v0, Vertex v1, Vertex v2)
		{
			int pos = items.Count;
			items.Add(Triangle.Create(vb.Add(v0), vb.Add(v1), vb.Add(v2)));
			return pos;
		}
		public void QuadTriangle(Vertex v0, Vertex v1, Vertex v2, Vertex v3)
		{
			var i0 = vb.Add(v0);
			var i2 = vb.Add(v2);
			items.Add(Triangle.Create(i0, vb.Add(v1), i2));
			items.Add(Triangle.Create(i0, i2, vb.Add(v3)));
		}
		public int[] ToTriangleStripIndexBufferArray()
		{
			List<int> strip = new List<int>();
			throw new System.NotImplementedException();
			return strip.ToArray();
		}

		public int[] ToTriangleIndexBufferArray()
		{
			int[] res = new int[TriangleCount * 3];
			int j = 0;
			for (int i = 0; i < res.Length; )
			{
				res[i] = items[j].Index0;
				++i;
				res[i] = items[j].Index1;
				++i;
				res[i] = items[j].Index2;
				++i;
				++j;
			}
			return res;
		}

	

		/// <summary>
		/// Returns an enumerator that iterates through a collection.
		/// </summary>
		/// <returns>
		/// An <see cref="T:System.Collections.IEnumerator"></see> object that can be used to iterate through the collection.
		/// </returns>
		/// <filterpriority>2</filterpriority>
		IEnumerator IEnumerable.GetEnumerator()
		{
			return ToTriangleIndexBufferArray().GetEnumerator();
		}
	}
}